jQueryAPI.Menu = {};

/**
 * Initialises the main systray icon
 */
jQueryAPI.Menu.Initialize = function(){
	if (DebugMode) air.trace('Creating Menu');
	
	function iconLoadComplete ( event ) {
		if (air.NativeApplication.supportsSystemTrayIcon) {
			air.NativeApplication.nativeApplication.icon.bitmaps = new Array(event.target.content.bitmapData);
			jQueryAPI.Menu.CreateSystrayMenu();
		}
	}
	
	var iconLoader = new runtime.flash.display.Loader();
	iconLoader.contentLoaderInfo.addEventListener(air.Event.COMPLETE, iconLoadComplete);
	iconLoader.load(new air.URLRequest('app:/assets/images/jQueryAPIOffline.png'));
};

/**
 * Adds items to the systray menu
 */
jQueryAPI.Menu.CreateSystrayMenu = function(){
	air.NativeApplication.nativeApplication.icon.menu = new air.NativeMenu();
	air.NativeApplication.nativeApplication.icon.addEventListener('click', jQueryAPI.Menu.SystrayClickHandler);
	
	var menuItems = {
		'jqueryMenuItem': new air.NativeMenuItem("jQuery.com", false),
		'learningjqueryMenuItem': new air.NativeMenuItem("LearningjQuery.com", false),
		'splitterA': new air.NativeMenuItem("", true),
		'maximizeMenu': new air.NativeMenuItem("Show jQueryAPI", false),
		'hideMenu': new air.NativeMenuItem("Hide jQueryAPI", false),
		'updatesMenu': new air.NativeMenuItem("Check For Updates", false),
		'optionsMenu': new air.NativeMenuItem("Set Options", false),
		'splitterB': new air.NativeMenuItem("", true),
		'closeMenu': new air.NativeMenuItem("Exit", false)
	}
		
	jQuery.each(menuItems, function(i, menuItem) {
		air.NativeApplication.nativeApplication.icon.menu.addItem(menuItem);
		menuItem.addEventListener(air.Event.SELECT, jQueryAPI.Menu.MenuItemClickHandler);
	});
};

jQueryAPI.Menu.SystrayClickHandler = function(event){

};

/**
 * Handles the click events from the systray menu and assigns functions
 * @param {Object} event The passed event
 */
jQueryAPI.Menu.MenuItemClickHandler = function(event){
	switch (event.target.label) {
		case "jQuery.com":
			air.navigateToURL(new air.URLRequest('http://jquery.com'));
		break;
		case "LearningjQuery.com":
			air.navigateToURL(new air.URLRequest('http://learningjquery.com'));
		break;
		case "Show jQueryAPI":
			jQueryAPI.Window.ShowHideAllWindows('show');
		break;
		case "Hide jQueryAPI":
			jQueryAPI.Window.ShowHideAllWindows('hide');
		break;
		case "Set Options":
			jQueryAPI.Window.ShowOptionsWindow();
		break;
		case "Exit":
			air.NativeApplication.nativeApplication.exit();
		break;
	}
  return;
};